package juc.thread;

public class TestThreadMethod {
    public static int flag=0;
    public static void main(String[] args) {
      test3();
    }
    public static void test3(){
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag=10;
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("..."+flag);

    }
    public static void test2(){
        Runnable runnable1 = () -> {
           int count=0;
           while (true){
               System.out.println("1:"+(++count));
           }
        };
        Runnable runnable2 = () -> {
            int count=0;
            while (true){
//                Thread.yield();//将当前cpu让出 重新转为runnable状态竞争cpu
                System.out.println("          2:"+(++count));
            }
        };
        Thread thread = new Thread(runnable1);
        Thread thread1 = new Thread(runnable2);
        thread.start();
        thread1.start();
        thread.setPriority(Thread.MAX_PRIORITY);
        thread1.setPriority(Thread.MIN_PRIORITY);

    }
    public static void test1(){
        Thread thread = new Thread("t1") {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    System.out.println("被打断...");
                    e.printStackTrace();
                }
                System.out.println("running，，，");
            }
        };
        thread.start();
        System.out.println(thread.getState());
//        thread.start();//IllegalThreadStateException
        for (int i = 0; i <100 ; i++) {//等上一会
            i++;
        }
        System.out.println(thread.getState());
        thread.interrupt();
        for (int i = 0; i <100 ; i++) {//等上一会
            i++;
        }
        System.out.println(thread.getState());
    }
}
